//
//  NetWorkingOrder.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/30.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "NetWorkingOrder.h"

@implementation NetWorkingOrder

//商品订单确认
+(void)ordersConfirmWithParams:(NSDictionary *)params withSuccessBlock:(void(^)(id responseObj))successBlock{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置超时
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    [manager GET:ProductOrderConfirm parameters:params headers:[NetWorkTool getUserToken] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"code"] intValue] == 500400) {
            [[XNProgressHUD shared] showErrorWithTitle:@"请重新登录"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [Configuration loginClick];
            });
            return;
        }
        if ([responseObject[@"code"] intValue] != 200) {
            [[XNProgressHUD shared] showErrorWithTitle:responseObject[@"msg"]];
            return;
        }
        successBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error.code  == -1001) {
            [[XNProgressHUD shared] dismiss];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[XNProgressHUD shared] showErrorWithTitle:@"请求超时,请刷新后再试！"];
            });
            return;
        }
        [[XNProgressHUD shared] showErrorWithTitle:@"网络错误"];
    }];
}


//商品订单下单
+(void)placeOrdersWithParams:(NSDictionary *)params withSuccessBlock:(void(^)(id responseObj))successBlock{
    [NetWorkTool requestTokenPostWithUrl:PlaceProductOrder WithParams:params withSuccessBlock:^(id  _Nonnull responseObj) {
        [[XNProgressHUD shared] dismiss];
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] dismiss];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[XNProgressHUD shared] showErrorWithTitle:errorStr];
        });
    }];
}

//商品订单页列表
+(void)getProductOrdersListWithStatus:(NSInteger)status withPage:(NSInteger)page withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%ld/%ld",ProductOrderList,(long)status,(long)page];
    [NetWorkTool requestTokenGetWithUrl:urlStr withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}


//会员商品订单页列表   1  待评价    2  售后
+(void)getProductItemListWithStatus:(NSInteger)status withPage:(NSInteger)page withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%ld/%ld",ProductorderItemList,(long)status,(long)page];
    [NetWorkTool requestTokenGetWithUrl:urlStr withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}

//会员商品订单页详情
+(void)getProductOrderItemDetail:(NSString *)itemId withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",ProductorderItemDetail,itemId];
    [NetWorkTool requestTokenGetWithUrl:urlStr withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}

//商品订单页详情
+(void)getProductOrderDetail:(NSString *)orderNo withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",ProductorderDetail,orderNo];
    [NetWorkTool requestTokenGetWithUrl:urlStr withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}

//确认收货
+(void)OrderReceiveConfirmWithOrderNo:(NSString *)orderNo withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",OrderReceiveConfirm,orderNo];
    [NetWorkTool requestTokenPostWithUrl:urlStr WithParams:@{} withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}

//取消订单
+(void)cancelOrderWithOrderNo:(NSString *)orderNo withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",ProductorderCancel,orderNo];
    [NetWorkTool requestTokenPostWithUrl:urlStr WithParams:@{} withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}

//确认删除
+(void)deleteOrderWithOrderNo:(NSString *)orderNo withSuccessBlock:(void(^)(id responseObj))successBlock{
    
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",ProductorderDelete,orderNo];
    [NetWorkTool requestTokenPostWithUrl:urlStr WithParams:@{} withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}

//支付状态  ProductorderState
+(void)getOrderStateWithOrderNo:(NSString *)orderNo withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",ProductorderState,orderNo];
    [NetWorkTool requestTokenPostWithUrl:urlStr WithParams:@{} withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}

//获取订单物流
+(void)getOrderTrackWithOrderNo:(NSString *)orderNo withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",ProductorderTrack,orderNo];
    [NetWorkTool requestTokenGetWithUrl:urlStr withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}
//去支付
+(void)orderPayWithOrderNo:(NSString *)orderNo withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",ProductorderPay,orderNo];
    [NetWorkTool requestTokenPostWithUrl:urlStr WithParams:@{@"source":@"app"} withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}

//确认完成
+(void)confirmDoneOrder:(NSString *)itemId withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",ProductorderItemDone,itemId];
    [NetWorkTool requestTokenPostWithUrl:urlStr WithParams:@{} withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}

//申请退货
+(void)applyOrderReject:(NSString *)itemId note:(NSString *)note withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",ProductorderItemReject,itemId];
    NSDictionary *dict = @{@"note":note};
    [NetWorkTool requestTokenPostWithUrl:urlStr WithParams:dict withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}
//申请退款
+(void)applyOrderRefund:(NSString *)itemId note:(NSString *)note withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",ProductorderItemRefund,itemId];
    NSDictionary *dict = @{@"note":note};
    [NetWorkTool requestTokenPostWithUrl:urlStr WithParams:dict withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}

//确认评价
+(void)submitCommentWithItemId:(NSString *)itemId withParams:(NSDictionary *)params withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",ProductorderItemComment,itemId];
    [NetWorkTool requestTokenPostWithUrl:urlStr WithParams:params withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}

//获取支付结果
+(void)getPayResultWithOrderNo:(NSString *)orderNo withSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@",ProductorderState,orderNo];
    [NetWorkTool requestTokenPostWithUrl:urlStr WithParams:@{} withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:errorStr];
    }];
}



@end





